package com.ruoyi.combat.service.useralignment.impl;

import java.io.IOException;
import java.util.*;

import com.ruoyi.combat.domain.request.AntiAlignmentRequest;
import com.ruoyi.combat.domain.useralignment.*;
import com.ruoyi.combat.mapper.useralignment.*;
import com.ruoyi.combat.service.externalServiceInvoker.NetworkUserIdentityProtectionService;
import com.ruoyi.combat.service.useralignment.ICombatAlignmentWarnService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 网络用户身份对齐告警Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-08
 */
@Service
public class CombatAlignmentWarnServiceImpl implements ICombatAlignmentWarnService
{
    @Autowired
    private CombatAlignmentWarnMapper combatAlignmentWarnMapper;
    @Autowired
    private NetworkUserIdentityProtectionService networkUserIdentityProtectionService;
    @Autowired
    private CombatHeavyUserMapper combatHeavyUserMapper;
    @Autowired
    private CombatRelationWbMapper combatRelationWbMapper;
    @Autowired
    private CombatRelationZhMapper combatRelationZhMapper;
    @Autowired
    CombatUserWbMapper combatUserWbMapper;
    @Autowired
    CombatUserZhMapper combatUserZhMapper;


    /**
     * 查询网络用户身份对齐告警
     * 
     * @param id 网络用户身份对齐告警主键
     * @return 网络用户身份对齐告警
     */
    @Override
    public CombatAlignmentWarn selectCombatAlignmentWarnById(Long id)
    {
        return combatAlignmentWarnMapper.selectCombatAlignmentWarnById(id);
    }

    @Override
    public Map getInfoByid(Long id) {
        Map re = new HashMap();
        CombatAlignmentWarn combatAlignmentWarn = combatAlignmentWarnMapper.selectCombatAlignmentWarnById(id);
        String userFromId = combatAlignmentWarn.getUserFromId();
        String userToId = combatAlignmentWarn.getUserToId();
        CombatHeavyUser combatHeavyUser = new CombatHeavyUser();
        combatHeavyUser.setWbUserId(userFromId);
        combatHeavyUser.setZhUserId(userToId);
        List<CombatHeavyUser> combatHeavyUsers = combatHeavyUserMapper.selectCombatHeavyUserList(combatHeavyUser);
        CombatHeavyUser combatHeavyUser1 = combatHeavyUsers.get(0);
        re.put("name", combatHeavyUser1.getName());
        re.put("unit", combatHeavyUser1.getUnit());
        CombatRelationWb combatRelationWb1 = new CombatRelationWb();
        combatRelationWb1.setUserid(userFromId);
        List<CombatRelationWb> combatRelationWbs = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb1);
        List<Map> weiboUserList = new ArrayList<>();
        for (CombatRelationWb combatRelationWb : combatRelationWbs){
            Map map = new HashMap();
            String userid = combatRelationWb.getUserid();
            String targetid = combatRelationWb.getTargetid();
            CombatUserWb combatUserWb = combatUserWbMapper.selectCombatUserWbByUserId(userid);
            CombatUserWb combatUserWb1 = combatUserWbMapper.selectCombatUserWbByUserId(targetid);
            map.put("source", combatUserWb);
            if(combatUserWb1==null) {
                combatUserWb1 = new CombatUserWb();
                combatUserWb1.setUserId(targetid);
                combatUserWb1.setUserName(combatRelationWb.getTargetName());
                combatUserWb1.setAvatar(combatRelationWb.getAvatar());
                List<CombatUserWb> children = new ArrayList<>();
                CombatRelationWb combatRelationWb2 = new CombatRelationWb();
                combatRelationWb2.setUserid(targetid);
                List<CombatRelationWb> list = combatRelationWbMapper.selectCombatRelationWbList(combatRelationWb2);
                for (CombatRelationWb relationWb : list){
                    String targetid1 = relationWb.getTargetid();
                    String targetName = relationWb.getTargetName();
                    String avatar = relationWb.getAvatar();
                    CombatUserWb combatUserWb2 = new CombatUserWb();
                    combatUserWb2.setUserId(targetid1);
                    combatUserWb2.setUserName(targetName);
                    combatUserWb2.setUserNickname(targetName);
                    combatUserWb2.setAvatar(avatar);
                    children.add(combatUserWb2);
                }
                combatUserWb1.setChildren(children);
            }
            map.put("target", combatUserWb1);
            weiboUserList.add(map);
        }
        re.put("weiboUserList",weiboUserList);
        CombatRelationZh zh = new CombatRelationZh();
        zh.setUserid(userToId);
        List<CombatRelationZh> combatRelationZhs = combatRelationZhMapper.selectCombatRelationZhList(zh);
        List<Map> zhihuUserList = new ArrayList<>();
        for (CombatRelationZh combatRelationWb : combatRelationZhs){
            Map map = new HashMap();
            String userid = combatRelationWb.getUserid();
            String targetid = combatRelationWb.getTargetid();
            CombatUserZh combatUserWb = combatUserZhMapper.selectCombatUserZhByUserId(userid);
            CombatUserZh combatUserWb1 = combatUserZhMapper.selectCombatUserZhByUserId(targetid);
            if(combatUserWb==null) {
                combatUserWb = new CombatUserZh();
                combatUserWb.setUserId(userid);
                combatUserWb.setUserName(combatRelationWb.getUsername());
            }
            map.put("source", combatUserWb);
            if(combatUserWb1==null) {
                combatUserWb1 = new CombatUserZh();
                combatUserWb1.setUserId(targetid);
                combatUserWb1.setUserName(combatRelationWb.getTargetName());
            }
            map.put("target", combatUserWb1);
            zhihuUserList.add(map);
        }
        re.put("zhihuUserList",zhihuUserList);
        return re;
    }

    /**
     * 查询网络用户身份对齐告警列表
     * 
     * @param combatAlignmentWarn 网络用户身份对齐告警
     * @return 网络用户身份对齐告警
     */
    @Override
    public List<CombatAlignmentWarn> selectCombatAlignmentWarnList(CombatAlignmentWarn combatAlignmentWarn)
    {
        return combatAlignmentWarnMapper.selectCombatAlignmentWarnList(combatAlignmentWarn);
    }

    /**
     * 新增网络用户身份对齐告警
     * 
     * @param combatAlignmentWarn 网络用户身份对齐告警
     * @return 结果
     */
    @Override
    public int insertCombatAlignmentWarn(CombatAlignmentWarn combatAlignmentWarn) {
        return combatAlignmentWarnMapper.insertCombatAlignmentWarn(combatAlignmentWarn);
    }

    /**
     * 修改网络用户身份对齐告警
     * 
     * @param combatAlignmentWarn 网络用户身份对齐告警
     * @return 结果
     */
    @Override
    public int updateCombatAlignmentWarn(CombatAlignmentWarn combatAlignmentWarn) {
        return combatAlignmentWarnMapper.updateCombatAlignmentWarn(combatAlignmentWarn);
    }

    /**
     * 批量删除网络用户身份对齐告警
     * 
     * @param ids 需要删除的网络用户身份对齐告警主键
     * @return 结果
     */
    @Override
    public int deleteCombatAlignmentWarnByIds(Long[] ids)
    {
        return combatAlignmentWarnMapper.deleteCombatAlignmentWarnByIds(ids);
    }

    /**
     * 删除网络用户身份对齐告警信息
     * 
     * @param id 网络用户身份对齐告警主键
     * @return 结果
     */
    @Override
    public int deleteCombatAlignmentWarnById(Long id)
    {
        return combatAlignmentWarnMapper.deleteCombatAlignmentWarnById(id);
    }

    @Override
    public String antiAlignment(AntiAlignmentRequest antiAlignmentRequest) {
        if (Objects.isNull(antiAlignmentRequest)) {
            throw new RuntimeException("参数为空");
        }
        if (StringUtils.isBlank(antiAlignmentRequest.getModified())) {
            throw new RuntimeException("缺少必输参数");
        }
        if ("1".equals(antiAlignmentRequest.getModified())) {
            // 判断参数是否为空
            if (StringUtils.isBlank(antiAlignmentRequest.getModified()) || StringUtils.isBlank(antiAlignmentRequest.getAlignedNode())
                    || antiAlignmentRequest.getAddNodeNum() == null || antiAlignmentRequest.getAddNodeNum() == 0) {
                throw new RuntimeException("缺少必输参数");
            }
        } else if ("2".equals(antiAlignmentRequest.getModified())) {
            if (antiAlignmentRequest.getSimilarNum() == null || antiAlignmentRequest.getSimilarNum() == 0 ||
                    StringUtils.isBlank(antiAlignmentRequest.getRatio()) ) {
                throw new RuntimeException("缺少必输参数");
            }
        }
        networkUserIdentityProtectionService.antiAlignment(antiAlignmentRequest.getModified(), antiAlignmentRequest.getAlignedNode(),
                antiAlignmentRequest.getAddNodeNum(), antiAlignmentRequest.getCallbackUrl(),
                antiAlignmentRequest.getSimilarNum(), antiAlignmentRequest.getRatio(), antiAlignmentRequest.getWeiboFilePath(),
                antiAlignmentRequest.getZhFilePath(), antiAlignmentRequest.getFileSimilarNodePath(),null,"");
        return null;
    }

    @Override
    public Map<String, Object> statistics() {

        // 统计所有的告警数量
        CombatAlignmentWarn combatAlignmentWarn = new CombatAlignmentWarn();
        int totalCount = combatAlignmentWarnMapper.selectCombatAlignmentWarnList(combatAlignmentWarn).size();

        // 查询状态为0，未处理的数量
        combatAlignmentWarn.setStatus("0");
        int unHandledCount = combatAlignmentWarnMapper.selectCombatAlignmentWarnList(combatAlignmentWarn).size();

        // 查询状态为2，已处理的数量
        combatAlignmentWarn.setStatus("2");
        int handledCount =  combatAlignmentWarnMapper.selectCombatAlignmentWarnList(combatAlignmentWarn).size();

        Map<String, Object> map = new HashMap<>();
        map.put("totalCount", totalCount);
        map.put("unHandledCount", unHandledCount);
        map.put("handledCount", handledCount);
        return map;
    }
}
